// 全局常量变量 start
const defaultHeaders = { "Content-Type": "application/json;charset=UTF-8" }

const defaultOptions = {
    timeout: 60000, //超时时间设置，单位毫秒
    url: "", // 请求的url
    type: "get", // 请求的类型
    dataType: "json", // 接收数据类型
    async: true, // 异步请求
    cache: false, // 浏览器缓存
    headers: defaultHeaders, // 请求头设置
};
// end


// 请求封装
function ajaxRequest(options, callback) {
    const activeOptions = Object.assign(defaultOptions, options);
    const { type } = activeOptions;
    switch (type) {
        case "get":
            activeOptions.data = activeOptions.params || activeOptions.data;
            break;
        case "post":
            activeOptions.data = JSON.stringify(activeOptions.data);
            break;
    }
    showLoading();

    // 加密逻辑
    const queryOptions = {}

    // promise 回调
    return new Promise((resolve, reject) => {
        $.ajax({
            ...activeOptions,
            success: function (result) {
                console.log("success: ", result);
                resolve(result);
            },
            error: function (error) {
                console.log("error: ", error);
                reject(error)
            },
            complete: function (params) {
                console.log("complete: ", params);
                hideLoading();
            },
        });
    })
}


// 编译全局loading dom
function compileLoading() {
    return $(`<div id="loadingId">
        <div style="" class="bt-spinner"></div>
        <style>
            #loadingId{
                width: 100%;
                height: 100%;
                display: flex;
                justify-content: center;
                align-items: center;
                position: absolute;
                left: 0;
                right: 0;
                top: 0;
                bottom: 0;
                margin: auto;
                z-index: 999;
                background: rgba(0,0,0,0.4);
            }
            .bt-spinner{
                width: 75px;
                height: 75px;
                border-radius: 50%;
                background-color: transparent;
                border: 4px solid #03a9f4;
                border-top-color: #1976d2;
                -webkit-animation: 1s spin linear infinite;
                animation: 1s spin linear infinite;
            }

            -webkit-@keyframes spin {
                -webkit-from {
                -webkit-transform: rotate(0deg);
                -ms-transform: rotate(0deg);
                transform: rotate(0deg);
                }
                -webkit-to {
                -webkit-transform: rotate(360deg);
                -ms-transform: rotate(360deg);
                transform: rotate(360deg);
                }
            }
            
            @-webkit-keyframes spin {
                from {
                -webkit-transform: rotate(0deg);
                transform: rotate(0deg);
                }
                to {
                -webkit-transform: rotate(360deg);
                transform: rotate(360deg);
                }
            }
            
            @keyframes spin {
                from {
                -webkit-transform: rotate(0deg);
                transform: rotate(0deg);
                }
                to {
                -webkit-transform: rotate(360deg);
                transform: rotate(360deg);
                }
            }
  
        </style>
    </div>`);
}


// 显示全局加载
function showLoading() {
    $("body").append(compileLoading());
}


// 隐藏全局加载
function hideLoading() {
    setTimeout(function () {
        $("#loadingId").remove();
    },3000)
}

